/**
 * Copyright (C) 2016 Eshore Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.eshore.thrpc.test.demo.xml;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.thrift.TException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.eshore.thrpc.test.gen.EchoService.Iface;

/**
 * The demo of client by xml file.
 * <p>
 * 
 * @author : zhouzhixiang
 * @date : 2015-6-9
 */
public class XmlClientDemo {

    /** spring配置文件 */
    private static final String SPRING_FILE_PATH = "demo/demo-client.xml";
    
    

    /**
     * @param args
     */
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        try {
			ApplicationContext context = new ClassPathXmlApplicationContext(SPRING_FILE_PATH);
            final Iface echoService = (Iface) context.getBean("echoService");
            int threads = 1;
//            final CountDownLatch begin = new CountDownLatch(1);
            final CountDownLatch latch = new CountDownLatch(threads);
            ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*10);
            long start = System.currentTimeMillis();
            for (int i = 0; i < 100000; i++) {
                echoService.echo("aaaa!");
                latch.countDown();
                /*executor.execute(new Runnable(){

                    @Override
                    public void run() {
                        try {
                            begin.await();
                            echoService.echo("world!");
                            latch.countDown();
                        } catch (TException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    
                });*/
            }
            
            
//            begin.countDown();
            latch.await();
            
            System.out.println("cost time:" +(System.currentTimeMillis()-start));
            
            
            Thread.sleep(20000);
            
            for(int i=0;i<5;i++){
                executor.execute(new Runnable(){

                    @Override
                    public void run() {
                        try {
                            String sss = echoService.echo("world!");
                            System.out.println(sss);
                        } catch (TException e) {
                            System.out.println("error echo to echoserver!!!");
                            e.printStackTrace();
                        }
                    }
                    
                });
            }
            
            System.in.read();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void start(int perCount){
        ApplicationContext context = new ClassPathXmlApplicationContext(SPRING_FILE_PATH);
        final Iface echoService = (Iface) context.getBean("echoService");
        
        for(int i=0;i<perCount;i++){
            try {
                echoService.echo("hello");
            } catch (TException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
